package com.bd.framework.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.bd.framework.utils.database.ConnectionFactory;
import com.bd.framework.utils.database.SQliteConnection;

public class InfoMachCompare {

	/**
	 * 根据ini的配置IP,查询远程数据库info_machine,更新本地的info_machine
	 * 同时比较本地ini文件中相关配置是否相同
	 * */
	public static String compareInfoMach(String ipAdress, String shopCode){
		String comFlag = "success";
		/**
		 * 先更新本地的info_machine 表
		 * */
		Connection sqliteCon = null,sybaseCon = null;
		Statement sqliteStatement = null,sybaseStatement = null;
		String infoMachSql = "SELECT * FROM INFO_MACHINE WHERE IPADDRESS="+"'"+ipAdress+"'";
		try { 
			sybaseCon = ConnectionFactory.getConnectionBASEDB();
			sqliteCon = (Connection) SQliteConnection.getSQliteConnection().getSQLiteCon();
			sybaseStatement = sybaseCon.createStatement();
			sqliteStatement = sqliteCon.createStatement();
			ResultSet rs = sybaseStatement.executeQuery(infoMachSql);
			while(rs.next()){
				//根据IP查询本地的info_machine
				String sqlQry = "SELECT * FROM INFO_MACHINE WHERE IPADDRESS="+"'"+
						ipAdress+"'";
				ResultSet sqlRs = sqliteStatement.executeQuery(sqlQry);
				if(rs.next()){
					if(sqlRs.getString("IPADDRESS") == null || sqlRs.getString("IPADDRESS").equals(null)){
						comFlag = "本机IP配置错误!";
					}else{
						//更新info_machine
						String upSql = "UPDATE INFO_MACHINE SET MACHINECODE ="+rs.getFloat("MACHINECODE")+",POSNAME='"+rs.getString("POSNAME")+
								"',CFLAG='"+rs.getString("CFLAG")+"',SHOPCODE='"+rs.getString("SHOPCODE")+"',MARKETCODE='"+rs.getString("MARKETCODE")+
								"',PRINTTYPE='"+rs.getString("PRINTTYPE")+"',PRINTFLAG='"+rs.getString("PRINTFLAG")+"',PRINTPORT="+rs.getFloat("PRINTPORT")+
								",DISPLAYPORT="+rs.getFloat("DISPLAYPORT")+",PRINTPAPER='"+rs.getString("PRINTPAPER")+"',MAGCARDBEGIN="+rs.getString("MAGCARDBEGIN")+
								"',MAGCARDOVER="+rs.getString("MAGCARDOVER")+"',OPERATE="+rs.getString("OPERATE")+",,,+USEFLAG='"+rs.getString("USEFLAG")+
								"',VALIDFLAG="+rs.getString("VALIDFLAG")+"' WHERE IPADDRESS='"+ipAdress+"'";
						System.out.println(upSql);
						sqliteStatement.executeUpdate(upSql);
					}
				}
				//校验ini的shopcode和远程是否一致,同时返回远程的shopcode
				if(rs.getString("SHOPCODE").equals(shopCode) || shopCode == rs.getString("SHOPCODE"))
					comFlag = "shopCode配置错误!";
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				sqliteCon.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally{
			try {
				//关闭本地SQlite连接
				sqliteStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		try {
			sqliteCon.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return comFlag;
	}

	public static void main(String[] args){
		String flag = InfoMachCompare.compareInfoMach("","");
		System.out.println(flag);
	}
}
